package com.wsb.controller;

import com.wsb.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class Logincontroller {
    @RequestMapping("/admin/login")
    public String adminLogin(){
        return "Login";
    }


    @Autowired
    LoginService loginService;

    @RequestMapping("/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp) {
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        //servlet层，职责  1.负责页面数据传递。2.调用service层实现业务逻辑。3.页面跳转
        //LoginService loginService=new LoginServiceImpl();
        boolean result = loginService.login(userCode, userPassword);
        if (result == true) {
            HttpSession httpSession = req.getSession();
            httpSession.setAttribute("userName", userCode);
            return ("jsp/frame");
        } else {
            HttpSession httpSession = req.getSession();
            httpSession.setAttribute("error", "用户名或密码错误");
            return "login";
        }
    }
    @RequestMapping("/jsp/logout.do")
    public String  loginOut(HttpSession httpSession) {
        httpSession.invalidate();
        return "login";
    }
}
